﻿using System.Linq;
using System.Web.Mvc;
using CodeCamp.Entities02;
using CodeCamp.Models.Schedule;
using CodeCamp.Models.Attendees;

namespace CodeCamp.Controllers
{
  public class AttendeesController : Controller
  {

    #region Picks

    public ActionResult Picks( string eventName )
    {
      string attendeeName = Session[ "AttendeeName" ] as string;

      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();

      Attendee attendee = null;
      if ( !string.IsNullOrEmpty( attendeeName ) )
      {
        attendee = repository.GetAttendee( eventId, attendeeName );
        if ( attendee == null )
          attendee = repository.CreateAttendee( eventId, attendeeName );
      }
      else
        attendee = repository.CreateAnonymousAttendee( eventId );

      Session[ "AttendeeName" ] = attendee.Name;

      var sessions =
        (
          from s in repository.GetSessions( eventId )
          orderby s.Title
          select new AttendeesPicksViewModel.Session { SessionId = s.Id, Title = s.Title }
        ).ToList();

      var pickedSessions =
        (
          from asp in attendee.AttendeeSessionPicks
          orderby asp.Session.Title
          select new AttendeesPicksViewModel.Session { SessionId = asp.SessionId, Title = asp.Session.Title }
        ).ToList();

      var model = new AttendeesPicksViewModel
      {
        AttendeeId = attendee.Id,
        IsAttendeeAnonymous = attendee.IsAnonymous,
        AttendeeName = attendee.Name,
        EventDisplayName = repository.GetEvent( eventId ).DisplayName,
        Sessions = sessions,
        PickedSessions = pickedSessions
      };

      return View( model );
    }

    [AcceptVerbs( HttpVerbs.Post )]
    public ActionResult PicksAnonymousAttendeeRename( AttendeesPicksViewModel model )
    {
      var repository = new CodeCampDataRepository();
      var attendee = repository.GetAttendee( model.AttendeeId );
      attendee.Name = model.AttendeeName;
      attendee.IsAnonymous = false;
      repository.Save();

      Session[ "AttendeeName" ] = attendee.Name;

      return RedirectToAction( "Picks" );
    }

    [AcceptVerbs( HttpVerbs.Post )]
    public ActionResult PicksChangeAttendee( AttendeesPicksViewModel model )
    {
      Session[ "AttendeeName" ] = model.AttendeeName;

      return RedirectToAction( "Picks" );
    }

    [AcceptVerbs( HttpVerbs.Post )]
    public ActionResult Picks( AttendeesPicksViewModel model, int[] pickedSessions )
    {
      var repository = new CodeCampDataRepository();
      var attendee = repository.GetAttendee( model.AttendeeId );
      var currentAttendeePickedSessions = attendee.AttendeeSessionPicks.ToList();

      // add newly picked sessions that are not in the currently picked sessions

      foreach ( var sessionId in pickedSessions )
      {
        int index = currentAttendeePickedSessions.FindIndex( ps => ps.SessionId == sessionId );

        if ( index == -1 )    // sessionId not in current picked sessions
          repository.Add( new AttendeeSessionPick { AttendeeId = attendee.Id, SessionId = sessionId } );
        else                  // sessionId in current picked sessions
          currentAttendeePickedSessions.RemoveAt( index );
      }

      // remove currently picked sessions that are not in the newly picked sessions

      foreach ( var attendeePickedSession in currentAttendeePickedSessions )
        repository.Delete( attendeePickedSession );

      // commit repository changes

      repository.Save();

      return RedirectToAction( "Picks" );
    }

    #endregion
  }
}
